/**
 * 仓库
 *
 * @author sxf
 * @email sxf02615@163.com
 * @date 2025/1/15
 */
package com.sxf.crm.repository;


import com.sxf.crm.entity.Customer;
import com.sxf.crm.enums.CustomerStage;
import com.sxf.crm.enums.CustomerStatus;
import com.sxf.crm.enums.DeleteStatus;
import com.sxf.crm.enums.CustomerSource;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface CustomerRepository extends JpaRepository<Customer, Long>, JpaSpecificationExecutor<Customer> {
    Page<Customer> findByNameContaining(String name, Pageable pageable);
    Page<Customer> findByStage(String stage, Pageable pageable);
    Page<Customer> findByStatus(String status, Pageable pageable);

    @Query("SELECT c FROM Customer c WHERE " +
           "(:name IS NULL OR c.name LIKE %:name%) AND " +
           "(:stage IS NULL OR c.stage = :stage) AND " +
           "(:status IS NULL OR c.status = :status) AND " +
           "c.deleteStatus = :deleteStatus")
    Page<Customer> findAll(
        @Param("name") String name,
        @Param("stage") CustomerStage stage,
        @Param("status") CustomerStatus status,
        @Param("deleteStatus") DeleteStatus deleteStatus,
        Pageable pageable
    );

    @Query("SELECT c FROM Customer c WHERE " +
           "(:name IS NULL OR c.name LIKE %:name%) AND " +
           "(:stage IS NULL OR c.stage = :stage) AND " +
           "(:status IS NULL OR c.status = :status)")
    Page<Customer> findByNameContainingAndStageAndStatus(
        @Param("name") String name,
        @Param("stage") String stage,
        @Param("status") String status,
        Pageable pageable
    );

    @Query("SELECT c FROM Customer c WHERE " +
           "(:name IS NULL OR c.name LIKE %:name%) AND " +
           "(:stage IS NULL OR c.stage = :stage) AND " +
           "(:source IS NULL OR c.source = :source) AND " +
           "(:status IS NULL OR c.status = :status)")
    Page<Customer> findByNameContainingAndStageAndSourceAndStatus(
        @Param("name") String name,
        @Param("stage") CustomerStage stage,
        @Param("source") CustomerSource source,
        @Param("status") CustomerStatus status,
        Pageable pageable
    );

    List<Customer> findByNameLikeIgnoreCase(String name);
    
    @Query("SELECT DISTINCT c FROM Customer c " +
           "JOIN c.customerDepartments cd " +
           "WHERE cd.department.id IN :departmentIds " +
           "AND (:query IS NULL OR c.name LIKE %:query%)")
    List<Customer> findByNameLikeIgnoreCaseAndDepartmentIds(
        @Param("query") String query, 
        @Param("departmentIds") List<Long> departmentIds
    );
    
    // 统计方法
    long countByStatus(CustomerStatus status);
} 